//
//  HMEmoticonPageView.swift
//  Weibo
//
//  Created by 李学林 on 15/12/15.
//  Copyright © 2015年 tete. All rights reserved.
//

//*****************************************************************************

import UIKit

class HMEmoticonPageView: UICollectionViewCell {
    // 当前 cell 显示的数据
    var emoticons: [HMEmoticon]? {
        didSet{
            
            // 先隐藏所有按钮
            for value in emoticonButtons {
                value.hidden = true
            }
            
            // 遍历表情数据的数组--> 设置一个表情数据显示一个表情按钮
            for (i,emoticon) in emoticons!.enumerate() {
                // 设置显示数据
                let button = emoticonButtons[i]
                button.hidden = false
                button.emoticon = emoticon
            }
        }
    }
    
    // 装有20个表情按钮的数组
    private lazy var emoticonButtons: [HMEmoticonButton] = [HMEmoticonButton]()
    
    
    var indexPath: NSIndexPath? {
        didSet{
            //label.text = "当前第\(indexPath!.section)组,第\(indexPath!.item)页\n从0开始"
            
            if indexPath?.section == 0  {
                recentUsedLabel.hidden = false
            }else{
                recentUsedLabel.hidden = true
            }
        }
    }
    
    // MARK: - 控件初始化界面调整
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        addEmoticonButtons()
        contentView.addSubview(deleteButton)
        // 添加最近使用过的表情下面的label
        contentView.addSubview(recentUsedLabel)
        
//        recentUsedLabel的约束
        recentUsedLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(self).offset(-10)
        }
        
    }
    
    
    
    /// 添加表情按钮
    private func addEmoticonButtons(){
        for _ in 0..<HMEmoticonsNumOfPage {
            let button = HMEmoticonButton()
            
            button.addTarget(self, action: "emoticonButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            
            button.titleLabel?.font = UIFont.systemFontOfSize(34)
            contentView.addSubview(button)
            emoticonButtons.append(button)
        }
    }
    
    // 调整20个按钮的大小与位置
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 拿到20个按钮,遍历设置 frame
        let leftMargin: CGFloat = 5
        let bottomMargin: CGFloat = 25
        
        // 计算表情按钮的宽度与高度
        let itemW = (width - leftMargin * 2) / CGFloat(HMEmoticonsColOfPage)
        let itemH = (height - 25) / CGFloat(HMEmoticonsRowOfPage)
        
        for (i, value) in emoticonButtons.enumerate() {
            
            // 设置宽高
            value.size = CGSize(width: itemW, height: itemH)
            // 设置位置
            
            // 求出当前按钮的行数与列数
            let col = i % HMEmoticonsColOfPage
            let row = i / HMEmoticonsColOfPage
            value.x = CGFloat(col) * itemW + leftMargin
            value.y = CGFloat(row) * itemH
        }
        
        // 调整删除按钮的位置.大小
        deleteButton.size = CGSize(width: itemW, height: itemH)
        deleteButton.x = width - itemW - leftMargin
        deleteButton.y = height - itemH - bottomMargin
        
    }
    
    // MARK: - 懒加载
    //    private lazy var label: UILabel = {
    //        let label = UILabel(textColor: UIColor.blackColor(), fontSize: 35)
    //        label.numberOfLines = 0
    //        label.textAlignment = .Center
    //        return label
    //    }()
    
    
    // MARK: - 监听方法emoticonButtonClick
    @objc private func emoticonButtonClick(button: HMEmoticonButton){
        // 取到这个按钮对应的表情数据
        // 发送通知
        NSNotificationCenter.defaultCenter().postNotificationName(HMEmoticonButtonDidSelectedNotification, object: nil, userInfo: ["emoticon": button.emoticon!])
    }
    

    
    
    // 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        return button
    }()
    private lazy var recentUsedLabel:UILabel = {
        let label = UILabel(textColor: UIColor.lightGrayColor(), textSize: 12)
        label.text = "最近使用过的表情"
        return label
    }()
}

//******************************************************************************